package com.easy.shiro.controller;

import com.easy.util.ValidationCode;
import com.github.bingoohuang.patchca.color.SingleColorFactory;
import com.github.bingoohuang.patchca.custom.ConfigurableCaptchaService;
import com.github.bingoohuang.patchca.filter.predefined.CurvesRippleFilterFactory;
import com.github.bingoohuang.patchca.font.RandomFontFactory;
import com.github.bingoohuang.patchca.utils.encoder.EncoderHelper;
import com.github.bingoohuang.patchca.word.RandomWordFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;

/**
 * @author wxk
 * @Description: 验证码生成
 * @date 2021/7/7
 */
@Controller
@RequestMapping
public class ValidationCodeController {
    @RequestMapping("/validCode")
    public void captcha(HttpServletRequest request, HttpServletResponse response,
                        @RequestParam(name = "w", defaultValue = "90") Integer width,
                        @RequestParam(name = "h", defaultValue = "38") Integer height,
                        @RequestParam(name = "n", defaultValue = "4") Integer number) throws IOException {
        ConfigurableCaptchaService configurableCaptchaService = new ConfigurableCaptchaService();
        configurableCaptchaService.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
        configurableCaptchaService
                .setFilterFactory(new CurvesRippleFilterFactory(configurableCaptchaService.getColorFactory()));
        RandomFontFactory randomFontFactory = new RandomFontFactory();
        randomFontFactory.setMinSize(30);
        randomFontFactory.setMaxSize(30);

        RandomWordFactory randomWordFactory = new RandomWordFactory();
        randomWordFactory.setMinLength(number);
        randomWordFactory.setMaxLength(number);

        configurableCaptchaService.setWordFactory(randomWordFactory);
        configurableCaptchaService.setFontFactory(randomFontFactory);
        configurableCaptchaService.setHeight(height);
        configurableCaptchaService.setWidth(width);

        response.setContentType("image/png");
        response.setHeader("Cache-Control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        long time = System.currentTimeMillis();
        response.setDateHeader("Last-Modified", time);
        response.setDateHeader("Date", time);
        response.setDateHeader("Expires", time);

        // 将VALIDCODE放入Session中
        ServletOutputStream stream = null;
        try {
            HttpSession session = request.getSession();
            stream = response.getOutputStream();
            String validate_code = EncoderHelper.getChallangeAndWriteImage(configurableCaptchaService,
                    "png", stream);
            session.setAttribute(ValidationCode.VALID_CODE_NAME, ValidationCode.create(validate_code));
            stream.flush();
        } finally {
            if (stream != null) {
                stream.close();
            }

        }
    }
}
